package com.jz.digital.caigou_shpiuliao

import com.jz.digital.MyAppLocation
import com.jz.digital.R
import com.jz.digital.base.BasePresenter
import com.jz.digital.bean.BaseEntity
import com.jz.digital.bean.ServerBean
import com.jz.digital.listener.ApiCallback
import com.jz.digital.net.DataApi
import com.jz.digital.utils.AccountManagerHelper
import com.jz.digital.utils.AppUtil
import com.jz.digital.utils.ErrorCode
import com.jz.digital.utils.GreenDaoUtils
import com.jz.digital.utils.GsonUtil
import com.jz.digital.utils.LogUtils
import kotlin.reflect.jvm.internal.impl.types.model.DefinitelyNotNullTypeMarker

/**
 * 采购收料
 */
class CaiGouShouLiaoPresenter : BasePresenter<ICaiGouShouLiaoView>() {
    private val tag = "CaiGouShouLiaoPresenter"
    val mDataList = mutableListOf<ServerBean.PurchasePOScanResponseTempModule>()
    var mScanResponse: MutableList<ServerBean.PurchasePOScanResponseTempModule>? = null
    private var wkId = ""
    private var wkName = ""

    init {
        val userInfo = GreenDaoUtils.getUserInfo(AccountManagerHelper.getInstance().userId)
        wkId = userInfo.userId
        wkName = userInfo.userName
    }
    /**
     * 查询获取临时表
     */
    fun queryTemp() {
        view?.showLoading()
        DataApi.api.queryTranTmp(ServerBean.TempBean(wkId)).enqueue(object :ApiCallback<BaseEntity<MutableList<ServerBean.PurchasePOScanResponseTempModule>?>>(){
            override fun onSuccess(response: BaseEntity<MutableList<ServerBean.PurchasePOScanResponseTempModule>?>?) {
                if (response != null && !response.data.isNullOrEmpty()) {
                    mDataList.clear()
                    mScanResponse = response.data
                    if (!response.data.isNullOrEmpty()) {
                        for (itemBean in response.data!!) {
                            mDataList.add(
                                ServerBean.PurchasePOScanResponseTempModule(
                                    id = itemBean.id,
                                    userId = itemBean.userId,
                                    prodNo = itemBean.prodNo,
                                    qty = itemBean.qty,
                                    price = itemBean.price,
                                    vendId = itemBean.vendId,
                                    vendName = itemBean.vendName,
                                    poNo = itemBean.poNo,
                                    dePo = itemBean.dePo,
                                    artId = itemBean.artId,
                                    specs = itemBean.specs
                                )
                            )
                        }
                    }
                    view?.notifyAdapter()
                }
                view?.dismissLoading()
            }

            override fun onError(t: Throwable?, code: Int, message: String?) {
                view?.dismissLoading()
            }

        })

    }
    /**
     *采购收料扫码
     */
    fun scan(
        barcode: String
    ) {
        view?.showLoading()
        val request=ServerBean.PurchasePOScanRequest(
            mode = 1,
            barcode = barcode,
            wk_id = wkId,
            wk_name = wkName,
            pda = AppUtil.getIMEI(MyAppLocation.mContext)
        )
        LogUtils.d(
            tag,
            "采购收料-开始查询扫描到的数据 request=${GsonUtil.toJson(request)}"
        )
        DataApi.api.purchasePOScan(
            request
        )
            .enqueue(object : ApiCallback<BaseEntity<ServerBean.PurchasePOScanResponse>>() {
                override fun onSuccess(response: BaseEntity<ServerBean.PurchasePOScanResponse>?) {
                    LogUtils.d(tag, "采购收料扫描成功 response=${GsonUtil.toJson(response)}")
                    if (response==null||response.data==null||response.data.list.isNullOrEmpty()){
                        if (response!=null&&response.data!=null&&!response.data.errmsg.isNullOrEmpty()){
                            view?.scanFailed(response.data.errmsg)
                        }
                        else{
                            view?.scanFailed(MyAppLocation.mContext.getString(R.string.scan_failed))
                        }
                    }
                    else{
                        mScanResponse=null
                        mDataList.clear()
                        if (response.code == ErrorCode.CODE_NOT_ALLOW_SIMULTANEOUS_RECEIPT){
                            view?.scanFailed(response.data.errmsg)
                        }
                        mScanResponse= response.data.list as MutableList<ServerBean.PurchasePOScanResponseTempModule>?
                        for (itemBean in response.data.list!!) {
                            mDataList.add(ServerBean.PurchasePOScanResponseTempModule(
                                id = itemBean.id,
                                userId = itemBean.userId,
                                prodNo = itemBean.prodNo,
                                qty = itemBean.qty,
                                price = itemBean.price,
                                vendId = itemBean.vendId,
                                vendName = itemBean.vendName,
                                poNo = itemBean.poNo,
                                dePo = itemBean.dePo,
                                artId = itemBean.artId,
                                specs = itemBean.specs
                            ))
                        }
                    }
                    view?.notifyAdapter()
                    view?.dismissLoading()
                }

                override fun onError(t: Throwable?, code: Int, message: String?) {
                    LogUtils.d(tag, "采购收料扫描失败 t=${t?.message},code=$code,message=$message")
                    view?.dismissLoading()
                }
            })
    }

    /**
     *采购收料提交
     */
    fun commit() {
        view?.showLoading()
        val request=ServerBean.PurchasePOCommitRequest(
            mode = 21, wk_id = wkId,
            pda = AppUtil.getIMEI(MyAppLocation.mContext)
        )
        LogUtils.d(tag, "采购收料-开始提交修改点 request=${GsonUtil.toJson(request)}")
        DataApi.api.purchasePOSubmit(request).enqueue(
            object : ApiCallback<BaseEntity<ServerBean.PurchasePOCommitResponse>>() {
                override fun onSuccess(response: BaseEntity<ServerBean.PurchasePOCommitResponse>?) {
                    LogUtils.d(tag, "采购收料 修改点提交成功 response=${GsonUtil.toJson(response)}")
                    mDataList.clear()
                    view?.dismissLoading()
                    view?.commitSuccess()
                }

                override fun onError(t: Throwable?, code: Int, message: String?) {
                    LogUtils.d(
                        tag,
                        "采购收料 修改点提交失败 t=${t?.message},code=$code,message=$message"
                    )
                    view?.dismissLoading()
                    view?.commitFailed(message)
                }
            }
        )
    }

    /**
     *删除单挑临时表
     */
    fun deleteTranTemp(position: Int, id: Long) {
        LogUtils.d(tag, "删除单挑临时表 $id")
        view?.showLoading()
        DataApi.api.deleteTranTemp(ServerBean.PurchasePODeleteRequest(id)).enqueue(object : ApiCallback<BaseEntity<Any>>() {
            override fun onSuccess(response: BaseEntity<Any>?) {
                LogUtils.d(tag, "单条临时表删除成功 id=$id response=${GsonUtil.toJson(response)}")
                mDataList.removeAt(position)
                view?.dismissLoading()
                view?.deleteTempSuccess()
            }

            override fun onError(t: Throwable?, code: Int, message: String?) {
                LogUtils.d(
                    tag,
                    "单条临时表删除失败 id=$id,t=${t?.message},code=$code,message=$message"
                )
                view?.dismissLoading()
                view?.deleteTempFailed(message)
            }
        })
    }

    fun updateTranPoTemp(id: Long, qty: Int, price: Float) {
        LogUtils.d(tag, "编辑单条临时表 id=$id,qty=$qty,price=$price")
        DataApi.api.updateTranPoTemp(
            ServerBean.UpdateTranPoTempRequest(
                id = id,
                qty = qty,
                price = price
            )
        )
            .enqueue(object : ApiCallback<BaseEntity<Any>>() {
                override fun onSuccess(response: BaseEntity<Any>?) {
                    LogUtils.d(tag, "临时表编辑成功 response=${GsonUtil.toJson(response)}")
                    view?.modifyTempSuccess()
                    view?.dismissLoading()
                }

                override fun onError(t: Throwable?, code: Int, message: String?) {
                    LogUtils.d(tag, "临时表编辑失败 t=${t?.message},code=$code,message=$message")
                    view?.modifyTempFailed(message)
                    view?.dismissLoading()
                }
            })
    }
}